package com.mengdie.shuidi.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mengdie.shuidi.AppConfig;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.helper.UIHelper;
import com.mengdie.shuidi.manager.MDAccountMgr;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by apple on 17/3/10.
 */

public class ExpireDialog extends DialogFragment {

    private static final String TAG = "ExpireDialog";
    @Bind(R.id.iv_continue_buy)
    ImageView mIvContinueBuy;
    @Bind(R.id.tv_close)
    TextView mTvClose;
    @Bind(R.id.tv_favorable_num)
    TextView mTvFavorableNum;
    private String mCoupoum;

    public static ExpireDialog newInstance( String num) {
        ExpireDialog dialog = new ExpireDialog();
        Bundle b = new Bundle();
        b.putString(TAG,num);
        dialog.setArguments(b);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCoupoum = getArguments().getString(TAG);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_pop_expire, null);
        builder.setView(view);
      //  builder.setCancelable(false);
        ButterKnife.bind(this, view);
        init();
        return builder.create();
    }

    private void init() {
        if (!TextUtils.isEmpty(mCoupoum)){
            mTvFavorableNum.setText("优惠码:"+mCoupoum);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_continue_buy, R.id.tv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_continue_buy:
                UIHelper.goWeb(getActivity(), AppConfig.getInstance().getAppInfo().getBuyUrl() + "?session_id=" +
                        MDAccountMgr.getInstance().getSessionId() + "&&coupon_num="+mCoupoum);
                dismiss();
                break;
            case R.id.tv_close:
                dismiss();
                break;
        }
    }
}
